X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/f8aec187ea7dc410a32996406109f290f3199ffa..2af83e98005a14c439b360a5b9ac636f594d9f0c:/Super%20Polarity/InputController.cs diff --git a/Super Polarity/InputController.cs b/Super Polarity/InputController.cs index 405b3ab..ad07717 100644 --- a/Super Polarity/InputController.cs +++ b/Super Polarity/InputController.cs @@ -11,6 +11,8 @@ namespace SuperPolarity static Dictionary>> Listeners; static Dictionary> RegisteredKeys; static Dictionary> RegisteredButtons; + static List BlockedKeys; + static List BlockedButtons; static GamePadState InputGamePadState; static KeyboardState InputKeyboardState; @@ -25,6 +27,10 @@ namespace SuperPolarity static InputController() { Listeners = new Dictionary>>(); + RegisteredButtons = new Dictionary>(); + RegisteredKeys = new Dictionary>(); + BlockedKeys = new List(); + BlockedButtons = new List(); InputKeyboardState = new KeyboardState(); InputGamePadState = new GamePadState(); } @@ -42,8 +48,82 @@ namespace SuperPolarity InputKeyboardState = Keyboard.GetState(); } + public static void RegisterEventForKey(string eventName, Keys key) + { + List newKeyList; + if (!RegisteredKeys.ContainsKey(eventName)) + { + newKeyList = new List(); + RegisteredKeys.Add(eventName, newKeyList); + } + + RegisteredKeys.TryGetValue(eventName, out newKeyList); + + newKeyList.Add(key); + } + + public static void RegisterEventForButton(string eventName, Buttons button) + { + List newButtonList; + if (!RegisteredButtons.ContainsKey(eventName)) + { + newButtonList = new List(); + RegisteredButtons.Add(eventName, newButtonList); + } + + RegisteredButtons.TryGetValue(eventName, out newButtonList); + + newButtonList.Add(button); + } + private static void DispatchRegisteredEvents() { + var keyFired = false; + + foreach (KeyValuePair> entry in RegisteredKeys) { + keyFired = false; + foreach (Keys key in entry.Value) + { + if (InputKeyboardState.IsKeyDown(key)) + { + if (!BlockedKeys.Contains(entry.Key)) + { + Dispatch(entry.Key, 1); + BlockedKeys.Add(entry.Key); + } + keyFired = true; + break; + } + } + + if (!keyFired) + { + BlockedKeys.Remove(entry.Key); + } + } + + foreach (KeyValuePair> entry in RegisteredButtons) + { + keyFired = false; + foreach (Buttons button in entry.Value) + { + if (InputGamePadState.IsButtonDown(button)) + { + if (!BlockedButtons.Contains(entry.Key)) + { + Dispatch(entry.Key, 1); + BlockedButtons.Add(entry.Key); + } + keyFired = true; + break; + }; + } + + if (!keyFired) + { + BlockedButtons.Remove(entry.Key); + } + } } private static void DispatchMoveEvents() @@ -55,8 +135,6 @@ namespace SuperPolarity xMovement = InputGamePadState.ThumbSticks.Left.X; yMovement = -InputGamePadState.ThumbSticks.Left.Y; - Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left)); - if (InputKeyboardState.IsKeyDown(Keys.Left)) { xMovement = -1.0f;